<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Http\Controllers;

use App\Models\Visitor;
use Illuminate\Http\Request;

class VisitorsController extends Controller
{
    public function index(Request $request)
    {
        $name = $request->get('name', false);
        $departure_date = $request->get('departure_date', false);
        $visitors = Visitor::query()
            ->when($name, function ($query, $name) {
                $query->where('name', 'like', "%{$name}%");
            })
            ->when($departure_date, function ($query, $departure_date) {
                $departure_date = formatDate($departure_date);
                $query->where('arrival_date', $departure_date);
            })
            ->latest('id')
            ->paginate($request->get('per_page', 10));
        return view(
            'visitors',
            compact(
                'visitors',
            )
        );
    }

    public function addVisitors(Request $request)
    {
        $sig = [
            'female' => '女士',
            'male' => '男士',
        ];
        $res = Visitor::query()->create([
            'name' => $request->get('name'),
            'gender' => $sig[$request->get('gender')] ?? '未填写',
            'id_card' => $request->get('idCard'),
            'departure_city' => $request->get('departureCity'),
            'departure_date' => $request->get('departureDate'),
            'arrival_date' => $request->get('arrivalDate'),
            'transport' => $request->get('transport'),
            'arrival_city' => $request->get('arrivalCity'),
            'flight_number' => $request->get('flightNumber'),
            'is_accommodation' => $request->get('isAccommodation'),
            'room_type' => $request->get('roomType'),
            'inviter' => $request->get('inviter'),
            'region' => $request->get('region'),
        ]);
        return responses($res);
    }
}
